package com.bw.lib_widget.verify_code

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import com.bw.lib_widget.R

/**
 * @author zhy
 * @date 2025-09-23 09:59
 * @description
 */
class VerifyCodeItemView: FrameLayout {
    private var cursor: View
    private var indicatorView: View
    private var numberView: TextView
    private val indicatorColor: Int //未选中指示器颜色
    private val indicatorFocusColor: Int //选中指示器颜色
    private val cursorColor:Int

    private var alphaAnimator: ValueAnimator? = null
    constructor(context: Context):this(context,null)
    constructor(context: Context, attrs: AttributeSet?):this(context,attrs,0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int):super(context,attrs,defStyleAttr){
        //view
        val parentView = LayoutInflater.from(context).inflate(R.layout.layout_verify_code_item,this)
        cursor = parentView.findViewById(R.id.view_cursor)
        indicatorView = parentView.findViewById(R.id.view_focus)
        numberView = findViewById(R.id.tv_number)
        //获得自定义属性
        val ty = context.obtainStyledAttributes(attrs, R.styleable.VerifyCodeItemView)
        cursorColor = ty.getColor(R.styleable.VerifyCodeItemView_cursorColor, Color.BLACK)
        indicatorColor = ty.getColor(R.styleable.VerifyCodeItemView_indicatorColor, Color.GRAY)
        indicatorFocusColor = ty.getColor(R.styleable.VerifyCodeItemView_indicatorFocusedColor, Color.BLACK)
        val numberSize = ty.getDimension(
            R.styleable.VerifyCodeItemView_numberSize,
            (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 25f, context.resources.displayMetrics))
        )
        ty.recycle()
        //view设置属性 :
        cursor.visibility = View.GONE
        cursor.setBackgroundColor(cursorColor)
        numberView.paint.textSize = numberSize
        indicatorView.setBackgroundColor(indicatorColor)
    }

    fun setNumber(number:String){
        numberView.text = number
    }

    /**
     * 设置是否选中
     */
    fun setFocus(focus:Boolean){
        if (focus){
            onFocus()
        }else{
            onUnFocus()
        }
    }

    fun onFocus(){
        indicatorView.setBackgroundColor(indicatorFocusColor)
        cursor.visibility = View.VISIBLE
        //属性动画：透明度
        alphaAnimator = ObjectAnimator.ofFloat(cursor, "alpha", 0f, 1f)
        alphaAnimator!!.duration = 1000
        alphaAnimator!!.repeatMode = ValueAnimator.RESTART
        alphaAnimator!!.repeatCount = -1
        alphaAnimator!!.start()

    }
    fun onUnFocus(){
        indicatorView.setBackgroundColor(indicatorColor)
        cursor.visibility = View.GONE
        if (alphaAnimator != null){
            alphaAnimator!!.cancel()
            alphaAnimator = null
        }
    }
}